<template>
	<view class="order">
		<view class="container">
			<view class="detail">
				<view class="top">
					<image :src="orderData.coachImg"></image>
					<view class="cont">
						<view class="title">{{orderData.title}}</view>
						<view class="price">
							<text class="symbol">¥</text>
							<text class="num">{{orderData.price}}</text>
							/节
						</view>
					</view>
				</view>
				<view class="change" v-if="type == 1">
					<view class="label">教练</view>
					<view class="cont">{{orderData.coachName}}</view>
				</view>
				<view class="change" v-if="type == 0">
					<view class="label"><text>*</text>日期</view>
					<view class="cont">
						<picker mode="date" :value="date" @change="bindDateChange">
							<view class="uni-input">{{date}}
								<uni-icons type="right" size="12" color="#A4A4A4"></uni-icons>
							</view>
						</picker>
					</view>
				</view>
				<view class="change">
					<view class="label"><text>*</text>时间</view>
					<view class="cont">
						<picker mode="time" :value="time" :start="startTime" :end="endTime" @change="bindTimeChange">
							<view class="uni-input">{{time}}
								<uni-icons type="right" size="12" color="#A4A4A4"></uni-icons>
							</view>
						</picker>
					</view>
				</view>
				<view class="change">
					<view class="label">地点</view>
					<view class="cont">{{orderData.address}}</view>
				</view>
			</view>
			<view class="changePay">
				<view class="title">支付方式</view>
				<radio-group @change="radioChange">
					<view v-for="(item, index) in payment" :key="item.value" class="box">
						<view class="label">{{item.name}}</view>
						<view class="cont">
							<radio :value="item.value" :checked="item.value == payStatusId" color="#E8232F" />
						</view>
					</view>
				</radio-group>
			</view>
		</view>

		<view class="footer">
			<view class="sum">
				<text class="des">合计 ¥ </text>
				<text class="num">{{orderData.price}}</text>
			</view>
			<button @click="createOrder">去支付</button>
		</view>
	</view>
</template>

<script>
	import {
		getLeagueDetail,
		wechatPay,
		payStatus,
		getCoachDetail
	} from "@/api/getData.js";
	export default {
		data() {
			return {
				orderData: {
					name: '王教练',
					imgUrl: 'https://cloudcdn.noitom.com.cn/noitom2/billiard/myIcon_h.png',
					title: '私教体验课',
					price: '166',
					address: 'xxxxxxxxxx',
					time: 0 //0-上午，1-下午
				},
				type: null,
				courseId: null,
				time: '请选择',
				date:'请选择',
				startTime: '09:00',
				endTime: '18:00',
				payment: [{
					value: '0',
					name: '余额'
				}, {
					value: '1',
					name: '微信'
				}],
				payStatusId: 0 //0-余额，1-微信
			}
		},
		onLoad(option) {
			console.log(option)
			this.type = option.type
			this.courseId = option.courseId
			// if (this.type == 0) {
			// 	this.startTime = '09:00'
			// 	this.endTime = '12:00'
			// } else {
			// 	this.startTime = '14:00'
			// 	this.endTime = '16:00'
			// }
			if(this.type == 1){
			this.getLeagueDetail()
			}else{
				this.getCoachDetail()
			}
		},
		methods: {
			async wechatPay() {
				var _this = this
				const param = {
					orderType: this.orderData.courseTypeId,
					courseScheduleId: Number(this.courseId),
					time: this.type == 0 ? this.date + ' ' + this.time : this.time,
					payType: this.payStatusId
				}
				const {
					data: res
				} = await wechatPay(param)
				if (res.data.isPay && this.payStatusId == 0) {
					uni.navigateTo({
						url: "/pages/pay/paymentSuccess?odatetime=" + res.data.odatetime + "&paymentNo=" + res.data.paymentNo + "&type=" + res.data.type + "&price=" + res.data.price + ""
					});
				} else if (this.payStatusId == 1) {
					wx.requestPayment({
						timeStamp: res.data.start.timestamp,
						nonceStr: res.data.start.nonceStr,
						package: res.data.pay.packageStr,
						signType: res.data.pay.signType,
						paySign: res.data.pay.paySign,
						success(req) {
							console.log(req)
							_this.payStatus(res.data.paymentId, res.data.start.timestamp, res.data.start
								.nonceStr,
								res.data.pay.packageStr, res.data.pay.paySign)

						},
						fail(e) {
							console.log(e)
						}
					})
				} else {
					uni.$showMsg(res.data.scheduleMsg, 1500, 'error')
				}
			},

			//支付结果上报
			async payStatus(paymentId, timeStamp, nonceStr, packages, paySign) {
				const param = {
					paymentId: paymentId,
					timeStamp: timeStamp,
					nonceStr: nonceStr,
					packageStr: packages,
					paySign: paySign,
				}
				const {
					data: res
				} = await payStatus(param)
				if (res.data) {
					uni.navigateTo({
						url: "/pages/pay/paymentSuccess?odatetime="+res.data.odatetime+"&paymentNo="+res.data.paymentNo+"&type="+res.data.type+"&price=" + res.data.price + ""
					});
				}

			},
			
			async getCoachDetail() {
				const {
					data: res
				} = await getCoachDetail(this.courseId)
				console.log(res)
				this.orderData = res.data
			},
			//详情
			async getLeagueDetail() {
				const {
					data: res
				} = await getLeagueDetail(this.courseId)
				console.log(res)
				this.orderData = res.data
			},
			//选择日期
			bindDateChange:function(e){
				this.date = e.detail.value
			},
			//选择时间
			bindTimeChange: function(e) {
				this.time = e.detail.value
			},
			//支付方式
			radioChange(e) {
				this.payStatusId = Number(e.detail.value)
				console.log(this.payStatusId)
			},
			createOrder() {
				// if (this.payStatusId == 0) {
				// 	//请求余额，如果不足跳转到payList
				// 	uni.navigateTo({
				// 		url: "/pages/pay/paymentSuccess"
				// 	});

				// } else {
				// 	//调起微信支付
				// 	uni.navigateTo({
				// 		url: "/pages/pay/paymentSuccess"
				// 	});
				// }
				this.wechatPay()
			}
		}
	}
</script>

<style lang="less">
	.order {
		.container {
			padding: 32rpx;
		}

		.detail {
			background: #FFFFFF;
			border-radius: 16rpx;
			padding: 32rpx 32rpx 0;

			.top {
				display: flex;

				image {
					display: block;
					width: 140rpx;
					height: 140rpx;
					margin-right: 20rpx;
				}

				.cont {
					line-height: 1;
					display: flex;
					flex-wrap: wrap;
					align-content: space-between;
					padding: 10rpx 0;

					.title {
						font-size: 32rpx;
						font-weight: bold;
						width: 100%;
					}

					.price {
						color: #A4A4A4;
						font-size: 20rpx;
						width: 100%;

						text {
							color: #000;
							color: #E60012;
						}

						.symbol {
							font-size: 20rpx;
						}

						.num {
							font-size: 36rpx;
							font-weight: bold;
						}
					}
				}
			}

			.change {
				height: 88rpx;
				display: flex;
				justify-content: space-between;
				align-items: center;
				border-bottom: 2rpx solid #E2E2E2;
				font-size: 24rpx;

				.label {
					font-weight: bold;

					text {
						color: #E8232F;
					}
				}

				.cont {
					color: #5B5B5B;
				}
			}

			.change:last-child {
				border: none;
			}
		}

		.changePay {
			background: #FFFFFF;
			border-radius: 16rpx;
			padding: 32rpx 32rpx 0;
			margin-top: 32rpx;

			.title {
				font-size: 32rpx;
				margin-bottom: 6rpx;
			}

			.box {

				height: 88rpx;
				display: flex;
				justify-content: space-between;
				align-items: center;
				border-bottom: 2rpx solid #E2E2E2;
				font-size: 24rpx;

				.label {
					font-weight: bold;

				}

				.cont {
					color: #5B5B5B;
				}
			}

			.box:last-child {
				border: none;
			}
		}

		.footer {
			height: 120rpx;
			background: #FFFFFF;
			overflow: hidden;
			display: flex;
			align-items: center;
			padding: 0 32rpx;
			position: fixed;
			width: 100%;
			left: 0;
			bottom: 0;
			box-sizing: border-box;

			.sum {
				font-size: 24rpx;
				color: #E60012;

				.num {
					font-weight: bold;
					font-size: 48rpx;
				}
			}

			button {
				width: 343rpx;
				height: 98rpx;
				background: #E60012;
				border-radius: 49rpx;
				padding: 0;
				line-height: 98rpx;
				font-size: 32rpx;
				font-weight: bold;
				color: #FFFFFF;
				margin-right: 0;
			}
		}
	}
</style>
